package com.sailfish.springbootdemo.service.db1;

import com.alibaba.fastjson.JSONObject;
import com.sailfish.springbootdemo.configs.UserHeaderHolder;
import com.sailfish.springbootdemo.dao.db1.*;
import com.sailfish.springbootdemo.pojo.Result;
import com.sailfish.springbootdemo.pojo.db1.LineTechBigType;
import com.sailfish.springbootdemo.pojo.db1.LineTechSmallType;
import com.sailfish.springbootdemo.pojo.db4.Operate;
import com.sailfish.springbootdemo.service.db4.OperateService;
import com.sailfish.springbootdemo.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class LineTechSmallTypeServiceImpl implements LineTechSmallTypeService {

    @Autowired
    private LineTechSmallTypeDao lineTechSmallTypeDao;

    @Autowired
    private LineTechBigTypeDao lineTechBigTypeDao;

    @Autowired
    private CommonJudgeCanDeleteService commonService;

    @Autowired
    private OperateService operateService;

    private String operateSmallType = "工艺整线类型";

    @Override
    public List<LineTechSmallType> getAllLineTechSmallTypeByBigType(String searchValue, Integer lineTechBigTypeId) {
        List<LineTechSmallType> list = lineTechSmallTypeDao.getAllLineTechSmallTypeByBigType(searchValue, lineTechBigTypeId);
        for (LineTechSmallType lineTechSmallType : list) {
            lineTechSmallType.setLineTechBigType(lineTechBigTypeDao.getLineTechBigTypeById(lineTechSmallType.getLineTechBigTypeId()));
        }
        return list;
    }

    @Override
    public Result addOrUpdateLineTechSmallType(LineTechSmallType lineTechSmallType, String mode) {
        try {
            if (lineTechSmallType == null) {
                return ResultUtil.error(500, "缺少参数lineTechSmallType", "Miss Param lineTechSmallType", null);
            }
            if (mode == null || "".equals(mode)) {
                return ResultUtil.error(500, "缺少参数mode", "Miss Param mode", null);
            }
            if (!("edit".equals(mode) || "add".equals(mode))) {
                return ResultUtil.error(500, "mode 参数错误", "mode Param Error", null);
            }

            String userId = UserHeaderHolder.getUserId();

            if ("edit".equals(mode)) {
                if (lineTechSmallType.getLineTechSmallTypeId() == null || "".equals(lineTechSmallType.getLineTechSmallTypeId().toString())) {
                    return ResultUtil.error(500, "缺少参数lineTechSmallTypeId", "Miss Param lineTechSmallTypeId", null);
                }

                List<LineTechBigType> lineTechBigTypeList = lineTechSmallTypeDao.judgeUniqueLineTechSmallType(lineTechSmallType.getLineTechBigTypeId(), lineTechSmallType.getLineTechSmallTypeId(), lineTechSmallType.getLineTechSmallTypeName());
                if (lineTechBigTypeList.size() > 0) {
                    return ResultUtil.error(500, "修改失败，存在相同名字", "Operate Fail", null);
                }

                LineTechSmallType oldLineTechSmallType = lineTechSmallTypeDao.getLineTechSmallTypeById(lineTechSmallType.getLineTechSmallTypeId());

                lineTechSmallType.setUpdateTime(new Date());
                Integer integer = lineTechSmallTypeDao.updateLineTechSmallType(lineTechSmallType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 3, 1, new Date(), JSONObject.toJSONString(oldLineTechSmallType), JSONObject.toJSONString(lineTechSmallType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            } else {

                List<LineTechSmallType> lineTechSmallTypeByNameAndBigType = lineTechSmallTypeDao.getLineTechSmallTypeByNameAndBigType(lineTechSmallType.getLineTechSmallTypeName(), lineTechSmallType.getLineTechBigTypeId());
                if (lineTechSmallTypeByNameAndBigType.size() > 0) {
                    return ResultUtil.error(500, "该项已存在", "Already Exist", null);
                }

                lineTechSmallType.setCreateTime(new Date());
                Integer integer = lineTechSmallTypeDao.addLineTechSmallType(lineTechSmallType);
                if (integer > 0) {
                    // 操作记录
                    operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 1, 1, new Date(), null, JSONObject.toJSONString(lineTechSmallType)));
                    return ResultUtil.success(null);
                }
                return ResultUtil.error(500, "操作失败", "Operate Fail", null);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }

    @Override
    public Result deleteLineTechSmallType(Integer lineTechSmallTypeId) {
        try {
            if (lineTechSmallTypeId == null || "".equals(lineTechSmallTypeId.toString())) {
                return ResultUtil.error(500, "缺少参数lineTechSmallTypeId", "Miss Param lineTechSmallTypeId", null);
            }

            String userId = UserHeaderHolder.getUserId();
            LineTechSmallType oldLineTechSmallType = lineTechSmallTypeDao.getLineTechSmallTypeById(lineTechSmallTypeId);

            Map<String, Integer> stringIntegerMap = commonService.judgeCanDeleteLineTechSmallType(lineTechSmallTypeId);
            if (stringIntegerMap.get("techCapacityRuleNum") > 0) {
                return ResultUtil.error(500,
                        "禁止删除，当前项" + stringIntegerMap.get("techCapacityRuleNum") + "处关联工艺整线能力规则",
                        "Forbidden Delete", null);
            }
            Integer integer = lineTechSmallTypeDao.deleteLineTechSmallType(lineTechSmallTypeId);
            if (integer > 0) {
                // 操作记录
                operateService.addOperate(new Operate(1, Integer.parseInt(userId), operateSmallType, 2, 1, new Date(), JSONObject.toJSONString(oldLineTechSmallType), null));
                return ResultUtil.success(null);
            }
            return ResultUtil.error(500, "操作失败", "Operate Fail", null);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultUtil.error(500, e.getMessage(), e.getMessage(), null);
        }
    }
}
